package com.jinmdz.fmis.dao.admin.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 组织机构Provider
 *
 * @author LiCongLu
 * @date 2020-03-16 10:51
 */
public class OrganizationProvider {

    /**
     * 查询组织机构列表
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-03-16 10:51
     */
    public String listOrganizationList() {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" organization_institution.id,organization_institution.parent_id,organization_institution.organization_code,organization_institution.organization_name ");
                builder.append(" ,organization_institution.organization_abbreviation,organization_institution.organization_describe,organization_institution.organization_type_code ");
                builder.append(" ,organization_institution.address,organization_institution.disable_state,organization_institution.sort,organization_institution.remark,organization_institution.version ");
                builder.append(" ,parent_institution.organization_code as parent_code,parent_institution.organization_name as parent_name");
                SELECT(builder.toString());
                FROM(" organization_institution ");
                LEFT_OUTER_JOIN(" organization_institution parent_institution on parent_institution.id = organization_institution.parent_id ");
                WHERE(" organization_institution.deleted = 0 ");
                ORDER_BY(" organization_institution.organization_code ");
            }
        }.toString();
    }

    /**
     * 按照父主键查询组织机构列表
     *
     * @param parentId 父类主键
     * @return
     * @author LiCongLu
     * @date 2020-03-16 10:51
     */
    public String listOrganizationListByParentId(@Param("parentId") Integer parentId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" organization_institution.id,organization_institution.parent_id,organization_institution.organization_code,organization_institution.organization_name ");
                builder.append(" ,organization_institution.organization_abbreviation,organization_institution.organization_describe,organization_institution.organization_type_code ");
                builder.append(" ,organization_institution.address,organization_institution.disable_state,organization_institution.sort,organization_institution.remark,organization_institution.version ");
                builder.append(" ,parent_institution.organization_code as parent_code,parent_institution.organization_name as parent_name");
                SELECT(builder.toString());
                FROM(" organization_institution ");
                LEFT_OUTER_JOIN(" organization_institution parent_institution on parent_institution.id = organization_institution.parent_id ");
                WHERE(" organization_institution.deleted = 0 ");
                WHERE(" organization_institution.parent_id = #{parentId} ");
                ORDER_BY(" organization_institution.organization_code ,organization_institution.sort");
            }
        }.toString();
    }

    /**
     * 按照主键获取组织机构信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-03-16 11:08
     */
    public String getOrganizationEditById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" organization_institution.id,organization_institution.parent_id,organization_institution.organization_code,organization_institution.organization_name ");
                builder.append(" ,organization_institution.organization_abbreviation,organization_institution.organization_describe,organization_institution.organization_type_code ");
                builder.append(" ,organization_institution.address,organization_institution.disable_state,organization_institution.sort,organization_institution.remark,organization_institution.version ");
                builder.append(" ,parent_institution.organization_code as parent_code,parent_institution.organization_name as parent_name");
                SELECT(builder.toString());
                FROM(" organization_institution ");
                LEFT_OUTER_JOIN(" organization_institution parent_institution on parent_institution.id = organization_institution.parent_id ");
                WHERE(" organization_institution.deleted = 0 ");
                WHERE("  organization_institution.id = #{id} ");
                ORDER_BY(" organization_institution.organization_code ");
            }
        }.toString();
    }
}
